package com.cms.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrSysRole;
import com.cms.model.TrSysRoleFun;
import com.cms.service.ReportService;
import com.cms.service.SystemRoleService;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

@Controller
public class RoleManagerController extends BaseController {

	@Autowired
	private SystemRoleService systemRoleService;

	@Autowired
	private ReportService reportService;

	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/roleManager.do")
	public String enter(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception {
		return "role/roleManagerMain.html";
	}

	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/roleManager/toRegRole.do")
	public String toRegRole(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception {
		map.put("action", "/roleManager/addRole.do");
		map.put("role", new TrSysRole());
		return "role/roleInfo.html";
	}

	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/roleManager/addRole.do")
	public void addRole(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception {
		TrAdminUser tu = this.getLoginUser(req);
		TrSysRole role = new TrSysRole();
		role.setRoleName(req.getParameter("roleName"));
		role.setRoleDesc(req.getParameter("roleDesc"));
		role.setCreateUser(tu.getId());
		role.setModifyUser(tu.getId());
		String data = req.getParameter("roleFunList");
		if (!StringUtils.isBlank(data)) {
			List<TrSysRoleFun> funs = new ArrayList<TrSysRoleFun>();
			TrSysRoleFun fun = null;
			JsonObject obj = null;
			JsonArray array = new JsonParser().parse(data).getAsJsonArray();
			for (int i = 0; array != null && i < array.size(); i++) {
				fun = new TrSysRoleFun();
				obj = array.get(i).getAsJsonObject();
				fun.setFunId(obj.get("funId").getAsString());
				fun.setFunName(obj.get("funName").getAsString());
				fun.setCreateUser(tu.getId());
				fun.setModifyUser(tu.getId());
				funs.add(fun);
			}
			role.setFuns(funs);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			this.systemRoleService.addSysRole(role);
			result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			result.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(result)); 
	}

	/**
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/roleManager/getFunctionList.do")
	public void getFunctionList(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map> funList = (List<Map>) this.reportService.getReportResult(
				"getRoleFunctionList", this.buildQueryMap(req));
		result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		result.put(CmsConstant.USER_FUNCTION_LIST, funList);
		this.renderText(resp, new Gson().toJson(result));
	}

	 /**
	  * 
	  * @param req
	  * @param resp
	  * @param map
	  * @return
	  * @throws Exception
	  */
	@RequestMapping("/roleManager/toEditRole.do")
	public String toEditRole(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception {
		int roleId = StringUtils.str2Int(req.getParameter("roleId"));
		map.put("action", "/roleManager/updateRole.do");
		map.put("role", this.systemRoleService.querySysRole(roleId));
		return "role/roleInfo.html";
	}

	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/roleManager/updateRole.do")
	public void updateRole(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception {
		TrAdminUser tu = this.getLoginUser(req);
		TrSysRole role = new TrSysRole();
		role.setRoleName(req.getParameter("roleName"));
		role.setRoleDesc(req.getParameter("roleDesc"));
		role.setId(StringUtils.str2Int(req.getParameter("roleId"))); 
		role.setModifyUser(tu.getId());
		String data = req.getParameter("roleFunList");
		if (!StringUtils.isBlank(data)) {
			List<TrSysRoleFun> funs = new ArrayList<TrSysRoleFun>();
			TrSysRoleFun fun = null;
			JsonObject obj = null;
			JsonArray array = new JsonParser().parse(data).getAsJsonArray();
			for (int i = 0; array != null && i < array.size(); i++) {
				fun = new TrSysRoleFun();
				obj = array.get(i).getAsJsonObject();
				fun.setFunId(obj.get("funId").getAsString());
				fun.setFunName(obj.get("funName").getAsString());
				fun.setCreateUser(tu.getId());
				fun.setModifyUser(tu.getId());
				funs.add(fun);
			}
			role.setFuns(funs);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			this.systemRoleService.updateSysRole(role);
			result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			result.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(result)); 
	}
	
	
	
	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/roleManager/pagingRoleList.do")
	public String pagingRoleList(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{
	   map.put("roleList", this.reportService.pagingReportResult("pagingRoleList", this.buildQueryMap(req)));
	   return "role/roleList.html";
	}
	
	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/roleManager/deleteRole.do")
	public void deleteRole(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception { 
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			this.systemRoleService.deleteSysRole(StringUtils.str2Int(req.getParameter("roleId")));
			result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			result.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(result)); 
	}
	
}
